package cn.exrick.xboot.modules.dataAnalysis.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.dataAnalysis.entity.DataAnalysis;
import cn.exrick.xboot.modules.dataAnalysis.service.IDataAnalysisService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Exrick
 */
@Slf4j
@RestController
@Api(description = "数据分析访问接口管理管理接口")
@RequestMapping("/xboot/dataAnalysis")
@Transactional
public class DataAnalysisController {

    @Autowired
    private IDataAnalysisService iDataAnalysisService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<DataAnalysis> get(@PathVariable String id){

        DataAnalysis dataAnalysis = iDataAnalysisService.getById(id);
        return new ResultUtil<DataAnalysis>().setData(dataAnalysis);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<DataAnalysis>> getAll(){

        List<DataAnalysis> list = iDataAnalysisService.list();
        return new ResultUtil<List<DataAnalysis>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<DataAnalysis>> getByPage(@ModelAttribute PageVo page){

        IPage<DataAnalysis> data = iDataAnalysisService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<DataAnalysis>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<DataAnalysis> saveOrUpdate(@ModelAttribute DataAnalysis dataAnalysis){
        dataAnalysis.setUpdateTime(new Date());
        if(iDataAnalysisService.saveOrUpdate(dataAnalysis)){
            return new ResultUtil<DataAnalysis>().setData(dataAnalysis);
        }
        return new ResultUtil<DataAnalysis>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){

        for(String id : ids){
            iDataAnalysisService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
    @RequestMapping(value = "/selectDataAnalysisPageVoMap", method = RequestMethod.GET)
    @ApiOperation(value = "数据分析分页获取数据")
    public Result<IPage<DataAnalysis>> selectDataAnalysisPageVoMap(@ModelAttribute Page page, @Param("url") String url, @Param("department") String department) {
        Map<String, Object> map = new HashMap<String, Object>();
        if(url!=null&&!url.equals("")&&url.length()!=0&&!url.trim().isEmpty()){
            map.put("url",url);
        }
        if(department!=null&&!department.equals("")&&department.length()!=0&&!department.trim().isEmpty()){
            map.put("department",department);
        }
        IPage<DataAnalysis> dataAnalysisIPage = iDataAnalysisService.selectDataAnalysisPageVoMap(page, map);

        return new ResultUtil<IPage<DataAnalysis>>().setData(dataAnalysisIPage);
    }

}
